package com.eascs.workflow.auditorReplace.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.dao.PageResult;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.web.sso.User;
import com.eascs.workflow.auditorReplace.service.IAuditorReplaceService;
import com.eascs.workflow.auditorReplace.vo.AuditorReVo;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.sys.dao.SysUserDao;

@RestController
@RequestMapping("/rAuditorRp")
public class AuditorReplaceRestController extends BaseRestController {
	
	@Autowired
	IAuditorReplaceService auditorReplaceService;
	
	@Autowired
	private SysUserDao sysUserDao;
	
	@RequestMapping("/list")
	public PaginationRestResult auditorList(AuditorReVo vo) {
		vo.setPageSize(10);
		String pageUrl = "/rAuditorRp/list";
		pageUrl = UrlUtil.appendUrl(pageUrl, "userNo", vo.getUserNo() == null ? "" : vo.getUserNo());
		pageUrl = pageUrl + "&pageIndex=";
		String titles[] = { "用户编号", "用户名称", "流程名称", "所属平台", "流程角色"};
		PageResult<Object[]> pageResult = auditorReplaceService.getauditorReVo(vo);
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl);
		Button replace = new AjaxButton("替换", "/rAuditorRp/view_replace?nodeAuditorId=") ;
		Button[] titleButton = new Button[] { replace };
		paginationInfo.setMoreButtons(titleButton);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setRendTo("#o2o_auditorReplace");
		ViewContext.put("vo", vo);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}
	
	
	@RequestMapping("/view_replace")
	public BaseRestResult<String> list(AuditorReVo vo) {
		vo.setPageSize(10);
		PageResult<Object[]> pageResult = auditorReplaceService.getauditorReVo(vo);
		 Object[] objects = pageResult.getData().get(0);
		 String auditor = objects[2] + "-" + objects[4] + "-" + objects[3] + "-" + objects[5];
		 vo.setUserName(auditor);
		 vo.setOrgId(objects[6]+"");
		 vo.setRoleNo(objects[7] + "");
		ViewContext.put("vo", vo);
		ViewContext.put("nodeRole", objects[5]);
		return HtmlRestResult.popHtmlFragment("/auditorReplace/replace");
	}
	
	/**
	 * 替换流程审核人
	 * @param vo
	 * @return
	 */
	@RequestMapping("/replace")
	public BaseRestResult<String> replaceAuditor(AuditorReVo vo) {
		
		if (StringUtil.isBlank(vo.getUserNo())) {
			return BaseRestResult.warn("流程审核人不能为空");
		}
		PageResult<Object[]> reAuditor = auditorReplaceService.getReAuditor(vo);
		
		if (reAuditor.getData().size() == 0) {
			return BaseRestResult.warn("替换人与被替换人角色权限不一致，不能替换");
		}
		User sysUser = sysUserDao.getSysUserById(vo.getUserNo());
		vo.setAuditorDes(vo.getUserName() + "-" + vo.getUserNo() + " " + "(+" + sysUser.getMobile() + ")");
		ModelResult<Boolean> replaceResult = auditorReplaceService.replaceAuditor(vo);
		if (!replaceResult.isSuccess()) {
			return BaseRestResult.warn("流程审核人替换失败");
		} 
		return BaseRestResult.info("流程审核人替换成功").andRefresh();
	}
	
	/**
	 * 联想流程审核人
	 * @param empName
	 * @return
	 */
	@RequestMapping(value = "/search/auditor")
	public BaseRestResult<List<Object[]>> employee(AuditorReVo vo) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(vo.getUserName())) {
			data = auditorReplaceService.getLikeEmployeeList(vo);
		}
		return BaseRestResult.buildRestResult(data);
	}
	
}
